package com.chinasofti.SSM.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigManager {
	//懒汉模式 
	/*private static ConfigManager configManager;*/
	//饿汉模式 - 初始化
	/*private static ConfigManager configManager = new ConfigManager();*/
	//饿汉模式 - 延时加载
	private static ConfigManager configManager;
	
	private static Properties props;
	private ConfigManager() {
		String configFile = "database.properties";
		props = new Properties();
		InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
		try {
			props.load(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 懒汉模式 - 线程不安全
	 * @return
	 */
	/*public static ConfigManager getInstance() {
		if(configManager == null) {
			configManager = new ConfigManager();
		}
		
		return configManager;
	}*/
	
	/**
	 * 懒汉模式 - 线程安全
	 * @return
	 */
	/*public static synchronized ConfigManager getInstance() {
		if(configManager == null) {
			configManager = new ConfigManager();
		}
		
		return configManager;
	}*/
	
	/**
	 * 饿汉模式 - 初始化
	 * @return
	 */
	/*public static ConfigManager getInstance() {
		return configManager;
	}*/
	
	/**
	 * 饿汉模式 - 延迟加载
	 * @return
	 */
	public static ConfigManager getInstance() {
		return ConfigManagerHelper.INSTANCE;
	}
	
	/*
	 * 静态内部类
	 */
	public static class ConfigManagerHelper {
		private static final ConfigManager INSTANCE = new ConfigManager();
	}
	
	public String getValue(String key) {
		return props.getProperty(key);
	}
}
